package com.songcha.sakura.ui.view.test

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import kotlin.math.absoluteValue

class TestTouchViewGroup
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : ViewGroup(context, attrs, defStyle, defRes) {

    private var mLastX=0f
    private var mLastY=0f


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        measureChildren(widthMeasureSpec,heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val child=getChildAt(0)
        val ml=(measuredWidth-child.measuredWidth)/2
        val mt=(measuredWidth-child.measuredWidth)/2
        child.layout(ml,mt,ml+child.measuredWidth,mt+child.measuredHeight)

    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
       // LogUtil.log("AAA","dispatchTouchEvent",ev)
        /*if(ev!=null){
            if (ev.action == MotionEvent.ACTION_DOWN) {
                mLastX = ev.x
                mLastY = ev.y

            }
        }*/

        return super.dispatchTouchEvent(ev)

    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
      //  LogUtil.log("AAA","onInterceptTouchEvent",ev)
        //return super.onInterceptTouchEvent(ev)

        //如果子view消费，判断父view是否需要拦截
        if(ev!=null){
            if(ev.action==MotionEvent.ACTION_DOWN) {
                mLastX = ev.x
                mLastY = ev.y
            }else if (ev.action == MotionEvent.ACTION_MOVE) {
                if((ev.x-mLastX).absoluteValue>(ev.y-mLastY).absoluteValue) {
                    return true
                }
            }
        }

        return super.onInterceptTouchEvent(ev)

    }

    override fun onTouchEvent(ev: MotionEvent?): Boolean {
      //  LogUtil.log("AAA","onTouchEvent",ev)
        //return super.onTouchEvent(ev)

       // return true
        if(ev!=null){
            //如果子view不消费，那么就由父view进行消费
            if(ev.action==MotionEvent.ACTION_DOWN){
               // mLastX = ev.x
               // mLastY = ev.y
                return true
            }
            else if (ev.action == MotionEvent.ACTION_MOVE) {
                if((ev.x-mLastX).absoluteValue>(ev.y-mLastY).absoluteValue) {
                 //   LogUtil.log("AAA","处理滑动")
                    return true

                }
            }
        }
        return super.onTouchEvent(ev)

        /*if (ev == null)
            return super.onTouchEvent(ev)
        if (ev.action == MotionEvent.ACTION_DOWN) {
            mLastX = ev.x
            mLastY = ev.y
            return true
        }
        else if (ev.action == MotionEvent.ACTION_MOVE) {

            if((ev.x-mLastX).absoluteValue>(ev.y-mLastY).absoluteValue) {
                return true

            }
        }
        return false*/
    }

}